package com.shanghai.shanghaielectric.UI.KeyBoard;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shanghai.shanghaielectric.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;



/**
 * Created by Administrator on 2017/9/2 0002.
 */

public class MKeyBoard_InputValue extends DialogFragment {
    public static final int CHANGEUI = 6262;
    public boolean isMinus=false;
    @BindView(R.id.et_errorAlert)
    TextView etErrorAlert;
    private StringBuffer buf;
    public Context mContext;
    public int maxInput = 0;
    public int minInput = 0;
    @BindView(R.id.et_input_min)
    TextView etInputMin;
    @BindView(R.id.et_input_max)
    TextView etInputMax;
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.topview)
    LinearLayout topview;
    @BindView(R.id.setting_center_key_one)
    TextView settingCenterKeyOne;
    @BindView(R.id.setting_center_key_two)
    TextView settingCenterKeyTwo;
    @BindView(R.id.setting_center_key_three)
    TextView settingCenterKeyThree;
    @BindView(R.id.setting_center_key_delete)
    TextView settingCenterKeyDelete;
    @BindView(R.id.setting_center_key_four)
    TextView settingCenterKeyFour;
    @BindView(R.id.setting_center_key_five)
    TextView settingCenterKeyFive;
    @BindView(R.id.setting_center_key_six)
    TextView settingCenterKeySix;
    @BindView(R.id.setting_center_key_clear)
    TextView settingCenterKeyClear;
    @BindView(R.id.setting_center_key_seven)
    TextView settingCenterKeySeven;
    @BindView(R.id.setting_center_key_eight)
    TextView settingCenterKeyEight;
    @BindView(R.id.setting_center_key_nine)
    TextView settingCenterKeyNine;
    @BindView(R.id.setting_center_key_dot)
    TextView settingCenterKeyDot;
    @BindView(R.id.setting_center_key_zero)
    TextView settingCenterKeyZero;
    @BindView(R.id.setting_center_key_sure)
    TextView settingCenterKeySure;
    @BindView(R.id.setting_center_key_minus)
    TextView settingCenterKeyMinus;

    public static interface MkeyBoardListener {
        void onClink(int value);
    }

    public void setMkeyBoardListener(MkeyBoardListener keyBoardListener) {
        mkeyBoardListener = keyBoardListener;
    }

    public static MkeyBoardListener mkeyBoardListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        View view = inflater.inflate(R.layout.m_keyboard_range_input, container);
        ButterKnife.bind(this, view);


//        Toast.makeText(mContext, this.getTag().toString(), Toast.LENGTH_SHORT).show();
        initView();
        initData();
        return view;
    }

    private void initView() {
        mContext = getContext().getApplicationContext();
        buf = new StringBuffer();
        etInput.setInputType(InputType.TYPE_NULL);
        etInputMin.setText(String.valueOf(minInput));
        etInputMax.setText(String.valueOf(maxInput));
    }

    private void initData() {

    }

    @OnClick({R.id.et_input_min, R.id.et_input_max, R.id.et_input, R.id.setting_center_key_one, R.id.setting_center_key_two, R.id.setting_center_key_three, R.id.setting_center_key_delete, R.id.setting_center_key_four, R.id.setting_center_key_five, R.id.setting_center_key_six, R.id.setting_center_key_clear, R.id.setting_center_key_seven, R.id.setting_center_key_eight, R.id.setting_center_key_nine, R.id.setting_center_key_dot, R.id.setting_center_key_zero, R.id.setting_center_key_sure,R.id.setting_center_key_minus})
    public void onViewClicked(View view) {
        Message message = new Message();

        if (view.getId() == R.id.setting_center_key_one) {
            buf.append(1);
//            Toast.makeText(mContext, "1", Toast.LENGTH_SHORT).show();
        } else if (view.getId() == R.id.setting_center_key_two) {
            buf.append(2);
//            Toast.makeText(mContext, "2", Toast.LENGTH_SHORT).show();
        } else if (view.getId() == R.id.setting_center_key_three) {
            buf.append(3);
        } else if (view.getId() == R.id.setting_center_key_four) {
            buf.append(4);
        } else if (view.getId() == R.id.setting_center_key_five) {
            buf.append(5);
        } else if (view.getId() == R.id.setting_center_key_six) {
            buf.append(6);
        } else if (view.getId() == R.id.setting_center_key_seven) {
            buf.append(7);
        } else if (view.getId() == R.id.setting_center_key_eight) {
            buf.append(8);
        } else if (view.getId() == R.id.setting_center_key_nine) {
            buf.append(9);
        } else if (view.getId() == R.id.setting_center_key_minus) {
           isMinus=!isMinus;
        } else if (view.getId() == R.id.setting_center_key_zero) {
            buf.append(0);
        } else if (view.getId() == R.id.setting_center_key_dot) {
            buf.append(".");
        } else if (view.getId() == R.id.setting_center_key_delete) {
            if (buf.length() > 0) {
                buf.delete(buf.length() - 1, buf.length());
            }
            if(buf.length()==0&&isMinus){
                isMinus=false;
            }
        } else if (view.getId() == R.id.setting_center_key_clear) {
            buf.delete(0, buf.length());
            if(buf.length()==0&&isMinus){
                isMinus=false;
            }
        } else if (view.getId() == R.id.setting_center_key_sure) {
            if (buf.length() > 0) {
                int inputValue = Integer.valueOf(buf.toString().trim());
                if (isMinus){
                    inputValue=-inputValue;
                }

                if (minInput <= inputValue && maxInput >= inputValue) {
                    //数值回调
                    if (mkeyBoardListener != null) {
                        mkeyBoardListener.onClink(inputValue);
                    }
                    dismiss();
                } else {

                    etErrorAlert.setVisibility(View.VISIBLE);
                }

            } else {
                dismiss();
            }
        }
        message.what = CHANGEUI;
        message.obj = buf.toString().trim();
        handler.sendMessage(message);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CHANGEUI:
                    if (isMinus){
                        etInput.setText("-"+buf.toString());
                    }else {
                        etInput.setText(buf.toString());
                    }
                    if (buf.length() > 0) {
                        int inputValue = Integer.valueOf(buf.toString().trim());
                        if (isMinus){
                            inputValue=-inputValue;
                        }
                        if (minInput <= inputValue && maxInput >= inputValue) {
                            etErrorAlert.setVisibility(View.INVISIBLE);
                        } else {
                            etErrorAlert.setVisibility(View.VISIBLE);
                        }
                    }else {
                        etErrorAlert.setVisibility(View.INVISIBLE);
                    }
                    break;
            }
        }
    };

    public void setInputValueRange(int min, int max) {

        maxInput = max;
        minInput = min;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        Toast.makeText(mContext, "销毁键盘", Toast.LENGTH_SHORT).show();

    }
}
